/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-28 16:08:51
 * @LastEditors: turingchang
 * @LastEditTime: 2020-09-04 10:47:08
 */


const Service = require('egg').Service;

class PermissionService extends Service {
    async list() {
        const role_list = await this.ctx.model.Admin.Role.findAll({
            include: {
                model: this.ctx.app.model.Admin.Admin
            },
            where:{
                status:this.ctx.app.enums.status.normal.value
            },
        });
        return role_list;
    }
    async get(id) {
        const permission = await this.ctx.model.Admin.Permission.findByPk(id,{
            attributes: { exclude: ['delete_time']},   
        });
        return permission;
    }
    async create(user) {
        const res =null;// await this.ctx.model.User.create(user);
        return res;
    }

    async menuTree(){
        // const admin = this.ctx.user;
        // const permissions = await this.ctx.model.Admin.Permission.findAll({
        //     attributes: { exclude: ['delete_time']},   
        //     limit: parms.page_size,
        //     offset: (parms.page_index - 1) * parms.page_size,         
        //     where:{
        //         show:'show',
        //     },
        //     order: [
        //         ['sort', 'desc'],
        //     ],
        //     distinct: true,
        // });    
        const permissions = await this.ctx.model.Admin.Permission.findAll({
            attributes: { exclude: ['delete_time']},      
            where:{
                // show:'show',
            },
            order: [
                ['sort', 'desc'],
            ],
            distinct: true,
        });  
        const menu = await this.getNode(permissions,0);     
        var data =[];
        var root={
            id:0,
            pid:0,            
            key:'index',
            title:'系统',
            component:'BasicLayout',
            path:'/',
            children:menu    
        }   
        data.push(root);
        return data;        
    }

    async getNode(permissions,pid){
        if(this.ctx.app.utils.validator.isNull(pid)){
            pid =0;
        }
        var menu ={};
        var data =[];
        var obj =permissions.filter(item => item.pid == pid && item.type == this.ctx.app.enums.permission_type.menu.value);
        for (const item of obj){
            menu ={
                id:item.id,
                pid:item.pid,
                key:item.key,
                title:item.name,
                slots: {
                    icon: item.icon,
                },
                type:item.type,
                component:item.component,
                path:item.path,
                redirect:item.redirect,
                children: await this.getNode(permissions,item.id),
                actions:permissions.filter(li => li.pid == item.id && li.type == this.ctx.app.enums.permission_type.action.value)
            }
            data.push(menu);  
        }
        return data;
    }   

    async getListByPid(parms){
        const permissions = await this.ctx.model.Admin.Permission.findAndCountAll({
            attributes: { exclude: ['delete_time']},      
            limit: this.ctx.limit,//parms.page_size,
            offset: this.ctx.offset,//(parms.page_index - 1) * parms.page_size,
            where:{
                pid:parms.pid
            },
            order: [
                ['sort', 'desc'],
            ],
            distinct: true,
            subQuery:false,
        });  
        return permissions;
    }

    async add(permission){
        const ctx =this.ctx;
        var obj ={
            result:false,
            response_code_type:ctx.app.enums.response_code_enum.param_error,
            msg:ctx.app.enums.response_code_enum.param_error.msg
        };
        if(ctx.app.utils.validator.isNull(permission)){
            return obj;
        }
        //数据校正
        if(ctx.app.utils.validator.isNull(permission.pid)){
            permission.pid =0;
        } 
        //检查key值是否重复 
        var keyexits =await this.exitsKey(permission.key);
        if(keyexits){
            obj.msg ='唯一键key值已存在';
            return obj;
        }
        var p = await this.ctx.model.Admin.Permission.create(permission);
        obj.result =true;
        obj.response_code_type =null;
        obj.msg ='添加成功';

        return obj;


    }
    
    /**
     * 检查权限项是否已存在
     * @param {sting} key 唯一键key
     */
    async exitsKey(key){
        var res =false;
        const ctx =this.ctx;
        const permission = await this.ctx.model.Admin.Permission.findOne({
            where:{
                key:key
            }
        });
        if(!ctx.app.utils.validator.isNull(permission)){
            res =true;
        }
        return res;
    }
  
}

module.exports = PermissionService;